﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Intex.Db.Service
{
    public interface IValueConvert
    {
        string Name { get; }

        string ExecuteToGetData(object data,Models.ItemType itemType, Models.TableItem row);

        string ExecuteToGetData(object data, Models.ItemType itemType, System.Data.DataRow row);
    }

    public class NumValueConvert : IValueConvert
    {
        public string Name
        {
            get { return "Num"; }
        }

        public string ExecuteToGetData(object data, Models.ItemType itemType, Models.TableItem row)
        {
            return convertToString(data, itemType);
        }

        public string ExecuteToGetData(object data, Models.ItemType itemType, System.Data.DataRow row)
        {
            return convertToString(data, itemType);
        }

        private string convertToString(object value, Models.ItemType itemType)
        {
            string vStr = value.ToString();
            var length = Convert.ToInt32(itemType.ItemLength);
            if (length < 1) return vStr;
            if (length < vStr.Length)
            {
                vStr = vStr.Substring(0, length);
            }
            else
            {
                for (var i = vStr.Length; i < length; i++)
                {
                    vStr = "0" + vStr;
                }
            }
            return vStr;
        }
    }

    public class DateValueConvert : IValueConvert
    {
        public string Name
        {
            get { return "Date"; }
        }

        public string ExecuteToGetData(object data, Models.ItemType itemType, Models.TableItem row)
        {
            return DateToString(data, itemType);
        }

        public string ExecuteToGetData(object data, Models.ItemType itemType, System.Data.DataRow row)
        {
            return DateToString(data, itemType);
        }

        private string DateToString(object data, Models.ItemType itemType)
        {
            DateTime date = Convert.ToDateTime(data);
            return date.ToString("yyyyMMdd");
        }
    }

    public class ImportDateValueConvert : IValueConvert
    {
        public string Name
        {
            get { return "ImportDataDate"; }
        }

        public string ExecuteToGetData(object data, Models.ItemType itemType, Models.TableItem row)
        {
            return DateTostring(data, itemType);
        }

        public string ExecuteToGetData(object data, Models.ItemType itemType, System.Data.DataRow row)
        {
            return DateTostring(data, itemType);
        }

        private string DateTostring(object date, Models.ItemType itemType)
        {
            try
            {
                DateTime dateTime = Convert.ToDateTime(date);
                return dateTime.ToString("yyyyMMdd");
            }
            catch (Exception)
            {
                return "";
            }
        }
    }

    public class StringValueConvert : IValueConvert
    {

        public string Name
        {
            get { return "Str"; }
        }

        public string ExecuteToGetData(object data, Models.ItemType itemType, Models.TableItem row)
        {
            return data.ToString();
        }

        public string ExecuteToGetData(object data, Models.ItemType itemType, System.Data.DataRow row)
        {
            return data.ToString();
        }
    }

    public class ImportNumValueConvert : IValueConvert
    {
        public string Name
        {
            get { return "ImportDataNum"; }
        }

        public string ExecuteToGetData(object data, Models.ItemType itemType, Models.TableItem row)
        {
            return data.ToString();
        }

        public string ExecuteToGetData(object data, Models.ItemType itemType, System.Data.DataRow row)
        {
            return data.ToString();
        }
    }

    public class ImportStrValueConvert : IValueConvert
    {
        public string Name
        {
            get { return "ImportDataStr"; }
        }

        public string ExecuteToGetData(object data, Models.ItemType itemType, Models.TableItem row)
        {
            return data.ToString();
        }

        public string ExecuteToGetData(object data, Models.ItemType itemType, System.Data.DataRow row)
        {
            return data.ToString();
        }
    }
}
